package es.xpt.ysf.commons.configuration.view;

import java.util.ArrayList;
import java.util.List;

import javax.faces.component.UIData;


import es.xpt.ysf.commons.configuration.model.SetupTask;
import es.xpt.ysf.logging.LogManager;
import es.xpt.ysf.utils.PackageInfo;
import es.xpt.ysf.view.Controller;

@SuppressWarnings("serial")
public class SetupTaskController extends Controller {
	
	private List<SetupTask> tasks;
	private UIData taskInfo;

	/**
	 * @return the taskInfo
	 */
	public UIData getTaskInfo() {
		return taskInfo;
	}

	/**
	 * @param taskInfo the taskInfo to set
	 */
	public void setTaskInfo(UIData taskInfo) {
		this.taskInfo = taskInfo;
	}

	public SetupTaskController() {
		super();
	}
	
	public List<SetupTask> getTasks() {
		if ( tasks == null ) {
			try {
				tasks = new ArrayList<SetupTask>();
				PackageInfo pi =new PackageInfo();
				List<Class<?>> lista = pi.getClasses("es.xpt");
				for ( int i = 0; i < lista.size(); i++ ) {
					for ( Class<?> c: lista.get(i).getInterfaces() ) {
						if ( c.equals(SetupTask.class) ) { // Clases que implementan SetupTask
							SetupTask p = (SetupTask)lista.get(i).newInstance();
							tasks.add(p);
							break;
						}
					}
				}
			} catch ( ClassNotFoundException e ) {
				LogManager.getLog(getClass()).error(e.getMessage(), e);
			} catch ( IllegalAccessException e ) {
				LogManager.getLog(getClass()).error(e.getMessage(), e);
			} catch ( InstantiationException e ) {
				LogManager.getLog(getClass()).error(e.getMessage(), e);
			}
		}
		return tasks;
	}

	/**
	 * @param tasks the tasks to set
	 */
	public void setTasks(List<SetupTask> tasks) {
		this.tasks = tasks;
	}

}
